using System;
using System.Collections.Generic;

namespace Meditek.UI
{
    public partial class SearchUserControl : System.Web.UI.UserControl
    {

        #region Search Event

        public class SearchEventArgs : EventArgs
        {
            public SearchEventArgs(string inDataValue)
            {
                this.ConditionString = inDataValue;
            }

            public readonly string ConditionString;
        }

        public delegate void SearchEventHandler(object sender, SearchEventArgs e);
        public event SearchEventHandler Search;

        protected virtual void OnSearch(SearchEventArgs e)
        {
            if (Search != null)
                Search(this, e);
        }

        #endregion

        #region Form Events

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string conditionString;
            switch (drlstOperators.SelectedItem.Text)
            {
                case "Contains":
                    conditionString = drlstMembers.SelectedValue + " Like '%" + txtValue.Text + "%'";
                    break;
                case "Start With":
                    conditionString = drlstMembers.SelectedValue + " Like '" + txtValue.Text + "%'";
                    break;
                case "End With":
                    conditionString = drlstMembers.SelectedValue + " Like '%" + txtValue.Text + "'";
                    break;
                case "Not Equals":
                    conditionString = drlstMembers.SelectedValue + " Not Like '%" + txtValue.Text + "'";
                    break;
                default:
                    conditionString = drlstMembers.SelectedValue + " Like '" + txtValue.Text + "'";
                    break;
            }
            OnSearch(new SearchEventArgs(conditionString));
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtValue.Text = string.Empty;
            drlstMembers.SelectedIndex = 0;
            drlstOperators.SelectedIndex = 0;
            btnSearch_Click(sender, e);
        }

        #endregion

        #region Public Functions

        public void AddMembers(Dictionary<string, string> _membersList)
        {
            drlstMembers.DataSource = _membersList;
            drlstMembers.DataBind();
        }

        #endregion

    }
}

